<template>
      <div>
        <el-form :label-position="labelPosition" :rules="rules" label-width="80px" :model="userForm" class="Loginbox">
            <h3>系统登录</h3>
          <el-form-item label="账号" prop="username">
            <el-input v-model="userForm.username"  placeholder="请输入账号" clearable maxlength="20" show-word-limit></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="userForm.password" placeholder="请输入密码" show-password clearable maxlength="20" show-word-limit></el-input>
          </el-form-item>
          <el-row>
            <el-button type="primary" style="width:100%" @click="login">登录</el-button>
          </el-row>
          <el-row>
            <router-link to="/user/regiter" class="goregiter">注册账号</router-link>
          </el-row>
        </el-form>
      </div>
</template>

<script>
  export default {
    data() {
      return {
        labelPosition: 'right',
        userForm: {
          username: "",
          password: "",
          
        },
        rules:{
          username:[
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password:[
          {required: true, message: '请输入密码', trigger: 'blur'},
        ]
        }
      };
    },
    methods:{

      login(){
        this.$http.post('login', this.userForm ).then( (res )=>{
          if( res.data || res){
                console.log(res)
                localStorage.setItem('UserToken' ,'Bearer ' + res.token //当axios拦截器不起作用使用  res.data.token
                )
            this.$router.push('/user/index')
            this.$message({
                message: '账号成功登陆',
                type: 'success'
              });
           }  
        }).catch( (res )=>{ 
          //如果连接不到服务器
          if(res.code == 'ERR_NETWORK'){
            this.$message.error('服务器异常');
            return;
          }
          //根据服务器返回内容 做出提示
          this.$message.error(res.response.data);
          //console.log(res.response.data)
          console.log(res)
        })       
        //console.log( this.userForm)   
      }
    }
  }
</script>

<style scoped>
.Loginbox{
    border-radius: 15px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 15px 35px 15px 35px;
    background-color: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
}
.goregiter{
  display: inline-block;
  text-decoration: none;
  margin-top: 20px;
  color:rgb(0, 129, 204)
}
</style>